//
//  GHLoginViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "THChangePwdViewController.h"
#import "UITextField+STTools_InputLimit.h"

@interface THChangePwdViewController ()<STTools_SendButtonDlegate,UITextFieldDelegate>
@property(nonatomic, strong) UITextField                     *accountTextFiled;
@property(nonatomic, strong) UITextField                     *codeTextFiled;
@property(nonatomic, strong) UITextField                     *pwdTextFiled;
@property(nonatomic, strong) UITextField                     *confimTextFiled;

@property(nonatomic, strong) STTools_SendButton                    *sendButton;/**<  */


@end
@implementation THChangePwdViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self stoneobs_fun_st_autoAdjustAllResponder];
    [self configSubView];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#define leftInset 24
#pragma mark --subView
- (void)configSubView{
    self.navigationController.navigationBar.hidden = YES;
    __weak typeof(self) weakSelf =  self;
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.navigationController.navigationBar.hidden = YES;
    self.cus_navgationBar.backgroundColor = UIColor.whiteColor;
    [self.cus_navgationBar.titleButton setTitle:@"修改密码" forState:UIControlStateNormal];
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 20)];

    CGFloat top = TMUtils.stoneobs_fun_get_navgationBarBootom;
  
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 80, 80)];
    imageView.image = [UIImage imageNamed:@"icon"];
    imageView.layer.cornerRadius = 8;
    imageView.clipsToBounds = YES;
    imageView.centerX = UISCREEN_WIDTH / 2;
    imageView.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    [header addSubview:imageView];
    
    self.accountTextFiled = [self textFiledWithLeftImage:@"手机号" placeHolader:@"请输入手机号"];
    self.accountTextFiled.top = imageView.bottom + 41 ;
    [header addSubview:self.accountTextFiled];
    top = self.accountTextFiled.bottom + 18;
    
    
    self.codeTextFiled = [self textFiledWithLeftImage:@"验证码" placeHolader:@"请输入验证码"];
    self.codeTextFiled.top = top;
    self.codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.codeTextFiled];
    top = self.codeTextFiled.bottom + 18;

    
    self.sendButton = [[STTools_SendButton alloc] initWithFrame:CGRectMake(0, 0, 80, 32) andWithDuration:60];
    self.sendButton.layer.cornerRadius = 16;
    self.sendButton.clipsToBounds = YES;
    self.sendButton.delegate = self;
    [self.sendButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [self.sendButton setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
    [self.sendButton setTitle:@"验证码" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.sendButton.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    
    self.sendButton.centerY = self.codeTextFiled.centerY;
    self.sendButton.right = self.codeTextFiled.right  -10;
    [header addSubview:self.sendButton];
    
    
    
    self.pwdTextFiled = [self textFiledWithLeftImage:@"请输入密码" placeHolader:@"请输入密码"];
    self.pwdTextFiled.top = top;
    [header addSubview:self.pwdTextFiled];
    self.pwdTextFiled.secureTextEntry = YES;
    top = self.pwdTextFiled.bottom + 18;
    
    self.confimTextFiled = [self textFiledWithLeftImage:@"请输入密码" placeHolader:@"请再次输入密码"];
    self.confimTextFiled.top = top;
    [header addSubview:self.confimTextFiled];
    self.confimTextFiled.secureTextEntry = YES;
    top = self.confimTextFiled.bottom + 18;
    


    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(36, self.confimTextFiled.bottom + 40, UISCREEN_WIDTH - 2 * 36, 44)
                                                  title:@"确认修改"
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:22
                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:button];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedForgetButton];
    }];
    
    
   
    header.height = button.bottom + 20;
    self.tableView.tableHeaderView = header;
    
}
- (UITextField*)textFiledWithLeftImage:(NSString*)imageName placeHolader:(NSString*)placeHolader{
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 24, 40)
                                                              title:nil
                                                         titleColor:nil
                                                          titleFont:0
                                                       cornerRadius:0
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:ST_IMAGE_NAMED(imageName)];
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(36, 0, UISCREEN_WIDTH - 0 - 36*2, 40)];
    [textFiled stoneobs_fun_setstoneobs_fun_st_leftView:stoneobs_ui_button];
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.placeholder = placeHolader;
    textFiled.backgroundColor = UIColor.whiteColor;
    textFiled.rightViewMode = UITextFieldViewModeAlways;
    textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    [textFiled stoneobs_fun_st_showBottomLine];
    return textFiled;
}
#pragma mark --UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{

}
#pragma mark --Action Method
- (void)onSelctedForgetButton{
    
    if (!self.accountTextFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入账号"];
        return;
    }
    if (!self.codeTextFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        return;
    }
    if (!self.pwdTextFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        return;
    }
    if (![self.pwdTextFiled.text isEqualToString:self.confimTextFiled.text]) {
        [SVProgressHUD showInfoWithStatus:@"两次密码输入不一致"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:self.accountTextFiled.text forKey:@"phone"];
    [stoneobs_net_param_dic setObject:self.pwdTextFiled.text forKey:@"password"];
    [stoneobs_net_param_dic setObject:self.codeTextFiled.text forKey:@"code"];
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/auth/resetPassword"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
    
}
#pragma mark --STSendButtonDlegate
- (BOOL)stoneobs_fun_st_sendButtonWillClic:(UIButton*)button{
    return YES;
}
- (void)stoneobs_fun_st_sendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    if (isFirstClic) {

        if (self.accountTextFiled.text.length != 11) {
            [SVProgressHUD showInfoWithStatus:@"请输入手机号"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [self.sendButton stoneobs_fun_st_timerEnd];
            });
            return;
        }

        [THUserDataController stoneobs_fun_send_get_code_request_phone:self.accountTextFiled.text handle:^(NSError *error, BOOL success, NSDictionary *resp) {
            if (success) {
                [self.sendButton stoneobs_fun_st_timerBegin];
            }
        }];
    }
}

- (void)stoneobs_fun_st_sendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:@(duration).description forState:UIControlStateNormal];
}
- (void)stoneobs_fun_st_sendButtonTimeEnded:(UIButton*)button{
    [self.sendButton setTitle:@"重新发送" forState:UIControlStateNormal];
    [self.sendButton stoneobs_fun_st_timerEnd];
}


@end


